using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using DevHub.WindowsApp.ViewModels.Pages;
using DevHub.WindowsApp.ViewModels.Pages.Code;
using DevHub.WindowsApp.ViewModels.Pages.Converter;
using DevHub.WindowsApp.ViewModels.Pages.Encode;
using DevHub.WindowsApp.ViewModels.Pages.Generate;
using DevHub.WindowsApp.ViewModels.Pages.Help;
using DevHub.WindowsApp.ViewModels.Pages.Music;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Windowing;

namespace DevHub.WindowsApp.Views;

public partial class MainWindow : AppWindow
{
    private Dictionary<string, PageModelBase> Stack { get; set; } = new()
    {
        // 软件相关
        { "Home", new HomeViewModel() },
        { "Plugin", new PluginViewModel() },
        { "Setting", new SettingViewModel() },
        { "Help", new HelpViewModel() },
        // 帮助
        { "Icon", new IconViewModel() },
        { "FontIcon", new FontIconViewModel() },
        // 转换
        { "Converter", new ConverterViewModel() },
        { "Translate", new TranslateViewModel() },
        { "BaseConverter", new BaseConverterViewModel() },
        { "RegexConverter", new RegexConverterViewModel() },
        //编解码
        { "Encode", new EncodeViewModel() },
        { "Base64Image", new Base64ImageViewModel() },
        { "Base64String", new Base64StringViewModel() },
        { "HtmlEncode", new HtmlEncodeViewModel() },
        { "GZipEncode", new GZipEncodeViewModel() },
        // 音乐
        { "Music", new MusicViewModel() },
        { "NetEasyMusic", new NetEasyMusicViewModel() },
        //文本编辑
        { "Edit", new EditViewModel() },
        { "Code", new CodeEditorViewModel() },
        { "Markdown", new MarkdownViewModel() },
        // 生成
        { "Generate", new GenerateViewModel() },
        { "HashGenerate", new HashGenerateViewModel() },
    };

    private WindowNotificationManager? Manager { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        TitleBar.ExtendsContentIntoTitleBar = true;
        TitleBar.TitleBarHitTestType = TitleBarHitTestType.Complex;

        FrameView.IsNavigationStackEnabled = true;
        FrameView.NavigationPageFactory = new NavigationFactory();
        Navigate("Home");
    }

    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        Manager = new WindowNotificationManager(this)
        {
            Position = NotificationPosition.BottomRight,
            MaxItems = 3
        };
    }

    #region Item

    private void ItemInvoked(object? sender, NavigationViewItemInvokedEventArgs e)
    {
        if (e.InvokedItemContainer.Tag is not string s) return;
        if (string.IsNullOrEmpty(s)) return;
        Navigate(s);
    }

    public void Navigate(string page)
    {
        if (string.IsNullOrEmpty(page)) return;
        Navigate(Stack[page]);
    }

    private void Navigate(object context)
    {
        FrameView.NavigateFromObject(context);
    }

    private void BackClick(object? sender, RoutedEventArgs e)
    {
        FrameView.GoBack();
    }

    #endregion

    #region Opeartion

    public async Task ShowWindow(object context)
    {
        var td = new TaskDialog
        {
            Title = "DevHub",
            Content = context,
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton,
                TaskDialogButton.CancelButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        await td.ShowAsync();
    }

    public async Task ShowWindow(TaskDialog dialog)
    {
        dialog.XamlRoot = (Visual)VisualRoot!;
        await dialog.ShowAsync();
    }

    public void CopyContext(string context, Notification? notification = null)
    {
        Clipboard?.SetTextAsync(context);
        if (notification != null) ShoeNotification(notification);
    }

    public void ShoeNotification(Notification notification)
    {
        Manager?.Show(notification);
    }

    #endregion
}